﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>557</ArticlesID><Title>Tạo Paging cho Repeater</Title><Body>&amp;lt;p&amp;gt;Như ch&amp;amp;uacute;ng ta đ&amp;amp;atilde; biết Repeater kh&amp;amp;ocirc;ng hỗ trợ Paging v&amp;amp;igrave; vậy ch&amp;amp;uacute;ng ta sẽ khắc phục n&amp;amp;oacute; bằng đoạn code sau:&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;aspx file&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: xhtml;&amp;quot;&amp;gt;
&amp;amp;lt;%@ Page Language=&amp;amp;quot;C#&amp;amp;quot; AutoEventWireup=&amp;amp;quot;true&amp;amp;quot; CodeFile=&amp;amp;quot;pagedrepeater.aspx.cs&amp;amp;quot; Inherits=&amp;amp;quot;PagedRepeater&amp;amp;quot; %&amp;amp;gt;
&amp;amp;lt;html&amp;amp;gt;
&amp;amp;lt;head&amp;amp;gt;&amp;amp;lt;title&amp;amp;gt;Paged Repeater&amp;amp;lt;/title&amp;amp;gt;
&amp;amp;lt;/head&amp;amp;gt;
&amp;amp;lt;body&amp;amp;gt;
&amp;amp;lt;form runat=&amp;amp;quot;server&amp;amp;quot;&amp;amp;gt;
      &amp;amp;lt;asp:Repeater ID=&amp;amp;quot;rptPages&amp;amp;quot; Runat=&amp;amp;quot;server&amp;amp;quot;&amp;amp;gt;
      &amp;amp;lt;HeaderTemplate&amp;amp;gt;
      &amp;amp;lt;table cellpadding=&amp;amp;quot;0&amp;amp;quot; cellspacing=&amp;amp;quot;0&amp;amp;quot; border=&amp;amp;quot;0&amp;amp;quot;&amp;amp;gt;
      &amp;amp;lt;tr class=&amp;amp;quot;text&amp;amp;quot;&amp;amp;gt;
         &amp;amp;lt;td&amp;amp;gt;&amp;amp;lt;b&amp;amp;gt;Page:&amp;amp;lt;/b&amp;amp;gt;&amp;amp;amp;nbsp;&amp;amp;lt;/td&amp;amp;gt;
         &amp;amp;lt;td&amp;amp;gt;
      &amp;amp;lt;/HeaderTemplate&amp;amp;gt;
      &amp;amp;lt;ItemTemplate&amp;amp;gt;
         &amp;amp;lt;asp:LinkButton ID=&amp;amp;quot;btnPage&amp;amp;quot;
                         CommandName=&amp;amp;quot;Page&amp;amp;quot;
                         CommandArgument=&amp;amp;quot;&amp;amp;lt;%#
                         Container.DataItem %&amp;amp;gt;&amp;amp;quot;
                         CssClass=&amp;amp;quot;text&amp;amp;quot;
                         Runat=&amp;amp;quot;server&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;%# Container.DataItem %&amp;amp;gt;
                         &amp;amp;lt;/asp:LinkButton&amp;amp;gt;&amp;amp;amp;nbsp;
      &amp;amp;lt;/ItemTemplate&amp;amp;gt;
      &amp;amp;lt;FooterTemplate&amp;amp;gt;
         &amp;amp;lt;/td&amp;amp;gt;
      &amp;amp;lt;/tr&amp;amp;gt;
      &amp;amp;lt;/table&amp;amp;gt;
      &amp;amp;lt;/FooterTemplate&amp;amp;gt;
      &amp;amp;lt;/asp:Repeater&amp;amp;gt;
      &amp;amp;lt;asp:Repeater ID=&amp;amp;quot;rptItems&amp;amp;quot; runat=&amp;amp;quot;server&amp;amp;quot;&amp;amp;gt;
      &amp;amp;lt;HeaderTemplate&amp;amp;gt;
      &amp;amp;lt;ul&amp;amp;gt;
      &amp;amp;lt;/HeaderTemplate&amp;amp;gt;
      &amp;amp;lt;ItemTemplate&amp;amp;gt;
      &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;%# Eval(&amp;amp;quot;pkItemID&amp;amp;quot;) %&amp;amp;gt;: &amp;amp;lt;%# Eval(&amp;amp;quot;Description&amp;amp;quot;) %&amp;amp;gt;&amp;amp;lt;/li&amp;amp;gt;
      &amp;amp;lt;/ItemTemplate&amp;amp;gt;
      &amp;amp;lt;FooterTemplate&amp;amp;gt;
      &amp;amp;lt;/ul&amp;amp;gt;
      &amp;amp;lt;/FooterTemplate&amp;amp;gt;
      &amp;amp;lt;/asp:Repeater&amp;amp;gt;
&amp;amp;lt;/form&amp;amp;gt;
&amp;amp;lt;/body&amp;amp;gt;
&amp;amp;lt;/html&amp;amp;gt;
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;.cs file&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: csharp;&amp;quot;&amp;gt;
using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class PagedRepeater : System.Web.UI.Page
{
   public int PageNumber
   {
      get
      {
         if (ViewState[&amp;amp;quot;PageNumber&amp;amp;quot;] != null)
            return Convert.ToInt32(ViewState[&amp;amp;quot;PageNumber&amp;amp;quot;]);
         else
            return 0;
      }
      set
      {
         ViewState[&amp;amp;quot;PageNumber&amp;amp;quot;] = value;
      }
   }
   protected override void OnInit(EventArgs e)
   {
      base.OnInit(e);
      rptPages.ItemCommand +=
         new RepeaterCommandEventHandler(rptPages_ItemCommand);
   }
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!Page.IsPostBack)
      LoadData();
   }
   private void LoadData()
   {
      SqlConnection cn = new SqlConnection(&amp;amp;quot;yourconnectiongoeshere&amp;amp;quot;);
      cn.Open();
      SqlDataAdapter da = new SqlDataAdapter(&amp;amp;quot;yourquerygoeshere&amp;amp;quot;, cn);
      DataTable dt = new DataTable();
      da.Fill(dt);
      cn.Close();
      PagedDataSource pgitems = new PagedDataSource();
      DataView dv = new DataView(dt);
      pgitems.DataSource = dv;
      pgitems.AllowPaging = true;
      pgitems.PageSize = 25;
      pgitems.CurrentPageIndex = PageNumber;
      if (pgitems.PageCount &amp;amp;gt; 1)
      {
         rptPages.Visible = true;
         ArrayList pages = new ArrayList();
         for (int i = 0; i &amp;amp;lt; pgitems.PageCount; i++)
         pages.Add((i + 1).ToString());
         rptPages.DataSource = pages;
         rptPages.DataBind();
      }
      else
         rptPages.Visible = false;
      rptItems.DataSource = pgitems;
      rptItems.DataBind();
   }
   void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
   {
      PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
      LoadData();
   }
}&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;VB.NET&amp;lt;/p&amp;gt;
&amp;lt;pre title=&amp;quot;code&amp;quot; class=&amp;quot;brush: vb;&amp;quot;&amp;gt;
Imports System
Imports System.Data
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Public Partial Class PagedRepeater
    Inherits System.Web.UI.Page
    Public Property PageNumber() As Integer
        Get
            If ViewState(&amp;amp;quot;PageNumber&amp;amp;quot;) IsNot Nothing Then
                Return Convert.ToInt32(ViewState(&amp;amp;quot;PageNumber&amp;amp;quot;))
            Else
                Return 0
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState(&amp;amp;quot;PageNumber&amp;amp;quot;) = value
        End Set
    End Property
    Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
        MyBase.OnInit(e)
        AddHandler rptPages.ItemCommand, AddressOf rptPages_ItemCommand
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Page.IsPostBack Then
            LoadData()
        End If
    End Sub
    Private Sub LoadData()
        Dim cn As New SqlConnection(&amp;amp;quot;yourconnectiongoeshere&amp;amp;quot;)
        cn.Open()
        Dim da As New SqlDataAdapter(&amp;amp;quot;yourquerygoeshere&amp;amp;quot;, cn)
        Dim dt As New DataTable()
        da.Fill(dt)
        cn.Close()
        Dim pgitems As New PagedDataSource()
        Dim dv As New DataView(dt)
        pgitems.DataSource = dv
        pgitems.AllowPaging = True
        pgitems.PageSize = 25
        pgitems.CurrentPageIndex = PageNumber
        If pgitems.PageCount &amp;amp;gt; 1 Then
            rptPages.Visible = True
            Dim pages As New ArrayList()
            For i As Integer = 0 To pgitems.PageCount - 1
                pages.Add((i + 1).ToString())
            Next
            rptPages.DataSource = pages
            rptPages.DataBind()
        Else
            rptPages.Visible = False
        End If
        rptItems.DataSource = pgitems
        rptItems.DataBind()
    End Sub
    Private Sub rptPages_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1
        LoadData()
    End Sub
End Class
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;Sưu tầm&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>10/27/2009 10:49:07 PM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2009/10/27/Tao-Paging-cho-Repeater-22D</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>26</CategoryId><ArrayCatID>26|25</ArrayCatID></Article></Articles>